package com.sjm.suchbear.easymap.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sjm.suchbear.easymap.Model.TaskModel;
import com.sjm.suchbear.easymap.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Suchbear on 2018/4/1.
 */
public class TaskAdapter extends BaseAdapter {
    private TaskModel taskModel;
    private Context context;

    public TaskAdapter(Context context, TaskModel taskModel) {
        this.context = context;
        this.taskModel = taskModel;
    }

    @Override
    public int getCount() {
        if (taskModel.getTasknum() > 0) {
            return taskModel.getTasknum();
        } else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return taskModel.getTasks().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvText.setText(taskModel.getTasks().get(position).getTaskName());
        if (taskModel.getTasks().get(position).getTaskFlag() == 0) {
            viewHolder.tvStatus.setText("未完成");
        } else if (taskModel.getTasks().get(position).getTaskFlag() == 1) {
            viewHolder.tvStatus.setText("已完成");
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_text)
        TextView tvText;
        @BindView(R.id.tv_status)
        TextView tvStatus;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
